using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class PlayShopComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private Texture2D _bgShop;
        private Vector2 _parallax;        
        public Vector2 Parallax
        {
            get { return _parallax; }
            set { _parallax = value; }
        }

        private Button _back;
        public Button Back
        {
            get { return _back; }
            set { _back = value; }
        }
        public Button _levelLuffy, _hpLuffy, _manaLuffy, _damLuffy, _balanceLuffy;
        private List<Button> _listIconTrap;
        private List<Button> _listTrapPrice;
        private List<Button> _listCountTrap;
        private Button _halfHp, _fullHp, _priceHalf, _priceFull;
        private int _priceStandard;

        //Initilation
        public PlayShopComponent(Game game, SpriteBatch spriteBatch, Texture2D bg, SpriteFont textFont, SpriteFont menuFont)
            : base(game)
        {
            _game = game;
            _spriteBatch = spriteBatch;
            _bgShop = bg;
            Parallax = Vector2.Zero;

            //Init list menu item
            _back = new Button("Back", textFont, new Vector2(620, 420), Color.Red);

            //Init Luffy info
            _levelLuffy = new Button(" ", textFont, new Vector2(50, 140), Color.Red) { HaveAnimation = false };
            _hpLuffy = new Button(" ", textFont, new Vector2(160, 60), Color.White) { HaveAnimation = false };
            _manaLuffy = new Button(" ", textFont, new Vector2(160, 100), Color.White) { HaveAnimation = false };
            _damLuffy = new Button(" ", textFont, new Vector2(160, 140), Color.White) { HaveAnimation = false };
            _balanceLuffy = new Button(" ", menuFont, new Vector2(430, 75), Color.Yellow) { HaveAnimation = false };

            //Init Buy Hp
            _halfHp = new Button(ManagerGame.managerTrap.ListIconInShop[0], new Vector2(110 , 195));
                    _halfHp.Selected += new Button.IsSelected(_halfHp_Selected);
             _priceHalf = new Button("  ", textFont, new Vector2(100, 270), Color.Yellow) { HaveAnimation = false };
            _fullHp = new Button(ManagerGame.managerTrap.ListIconInShop[1], new Vector2( 280, 195));
                    _fullHp.Selected += new Button.IsSelected(_fullHp_Selected);
             _priceFull = new Button("  ", textFont, new Vector2(280, 270), Color.Yellow) { HaveAnimation = false };
            
            // Init Trap
            _listIconTrap = new List<Button>();
            _listTrapPrice = new List<Button>();
            _listCountTrap = new List<Button>();
            int _indexY = 0;            int _indexX = -1;
            for (int i = 0; i < ManagerGame.managerTrap.ListIconInShop.Count - 2; i++)
            {
                _indexX++;
                if (i % 3 == 0) { _indexY++; _indexX = 0; }
                _listIconTrap.Add(new Button(ManagerGame.managerTrap.ListIconInShop[i + 2], new Vector2(35 + _indexX * 180, 185 + _indexY * 140)));
                _listIconTrap[i].Selected += new Button.IsSelected(BuyItem_Selected);
                _listTrapPrice.Add(new Button("$" + ManagerGame.managerTrap.ListAllTraps[i].Price.ToString(), textFont, new Vector2(35 + _indexX * 180, 265 + _indexY * 140),Color.Yellow) { HaveAnimation = false });
                _listCountTrap.Add(new Button("x" + ManagerGame.managerTrap.ListTrapsHave[i].ToString(), textFont, new Vector2(115 + _indexX * 180, 205 + _indexY * 140), Color.Black) { HaveAnimation = false });
            }
            
        }

        void _fullHp_Selected(int input)
        {
            if (ManagerGame.luffy.Balance >= _priceStandard)
            {
                ManagerGame.luffy.Hp = ManagerGame.luffy.Hpmax;
                ManagerGame.luffy.Balance -= _priceStandard;
            }
        }

        void _halfHp_Selected(int input)
        {
            if (ManagerGame.luffy.Balance >= (_priceStandard / 2))
            {
                ManagerGame.luffy.Hp += (int)(ManagerGame.luffy.Hpmax / 2);
                ManagerGame.luffy.Balance -= (int)(_priceStandard / 2);
            }
        }

        void BuyItem_Selected(int input)
        {
            // Dung mua trap
            if (ManagerGame.luffy.Balance >= ManagerGame.managerTrap.ListAllTraps[input].Price)
            {
                ManagerGame.luffy.Balance -= ManagerGame.managerTrap.ListAllTraps[input].Price;
                ManagerGame.managerTrap.AddTrapHave(input);
            }
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _priceStandard = (int)(ManagerGame.luffy.Hpmax * Math.Pow(1.06, ManagerGame.luffy.LevelScreen));
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    _back.ButtonIsTouched(gesture.Position, 0);
                    for (int i = 0; i < ManagerGame.managerTrap.ListIconInShop.Count - 2; i++)
                        _listIconTrap[i].ButtonIsTouched(gesture.Position, i);
                    _halfHp.ButtonIsTouched(gesture.Position, 0);
                    _fullHp.ButtonIsTouched(gesture.Position, 0);
                }
            }

            _levelLuffy.Text = "Lv " + ManagerGame.luffy.Level.ToString();
            _hpLuffy.Text = "Hp    : " + ManagerGame.luffy.Hp.ToString();
            _manaLuffy.Text = "Mana : " + ManagerGame.luffy.Mana.ToString();
            _damLuffy.Text = "Dam  : " + ManagerGame.luffy.Damage.ToString();
            _balanceLuffy.Text = ManagerGame.luffy.Balance.ToString();
            _priceHalf.Text = "$" + (_priceStandard / 2).ToString();
            _priceFull.Text = "$" + (_priceStandard).ToString();
            for (int i = 0; i < ManagerGame.managerTrap.ListIconInShop.Count - 2; i++)
                _listCountTrap[i].Text = "x" + ManagerGame.managerTrap.ListTrapsHave[i].ToString();
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ManagerGame.camera.GetViewMatrix(Parallax));
                base.Draw(gameTime);
                _spriteBatch.Draw(_bgShop,Vector2.Zero, Color.White);
                _back.Draw(_spriteBatch, gameTime);
                _levelLuffy.Draw(_spriteBatch, gameTime);
                _hpLuffy.Draw(_spriteBatch, gameTime);
                _manaLuffy.Draw(_spriteBatch, gameTime);
                _damLuffy.Draw(_spriteBatch, gameTime);
                _balanceLuffy.Draw(_spriteBatch, gameTime);
                for (int i = 0; i < ManagerGame.managerTrap.ListIconInShop.Count - 2; i++)
                {
                    _listIconTrap[i].Draw(_spriteBatch, gameTime);
                    _listTrapPrice[i].Draw(_spriteBatch, gameTime);
                }
                _halfHp.Draw(_spriteBatch, gameTime);
                _fullHp.Draw(_spriteBatch, gameTime);
                _priceHalf.Draw(_spriteBatch, gameTime);
                _priceFull.Draw(_spriteBatch, gameTime);
                for (int i = 0; i < ManagerGame.managerTrap.ListIconInShop.Count - 2; i++)
                    _listCountTrap[i].Draw(_spriteBatch, gameTime);
            _spriteBatch.End();
        }
    }
}
